package com.geovis.emergency.spd.controller.pc;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.constant.SwaggerConstants;
import com.geovis.common.core.controller.BaseController;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.emergency.spd.biz.pc.service.IPcMessageService;
import com.geovis.emergency.spd.entity.pc.entity.PcMessage;
import com.geovis.emergency.spd.entity.pc.pojo.constant.PcMessageConstant;
import com.geovis.emergency.spd.entity.pc.pojo.dto.PcMessageSaveDTO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcMessageVO;
import com.geovis.emergency.spd.util.MsgUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 应急预案指挥模块-消息通知 前端控制器
 * </p>
 *
 * @author 曾德实
 * @since 2022-04-30
 */
@RestController
@RequestMapping("/pc/pcMessage")
@RequiredArgsConstructor
@Api(value = "应急预案指挥模块-消息通知接口", tags = "应急预案指挥模块-消息通知接口")
@Slf4j
@Validated
public class PcMessageController extends BaseController<IPcMessageService> {

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = SwaggerConstants.LIST_PAGE_VALUE, notes = SwaggerConstants.LIST_PAGE_VALUE)
    @PostMapping("/listPage")
    public Result<PageResult<PcMessageVO>> listPage(@RequestBody @Validated PageParam<PcMessage> pageParam) {
        return Result.ok(baseService.listPage(pageParam));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "一键已读消息", notes = "一键已读消息")
    @PostMapping("/oneKeyRead")
    public Result oneKeyRead(@RequestParam(value = "msgId",required = false) String messageId) {
        baseService.onKeyRead(messageId);
        return Result.ok();
    }
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "未读消息数量", notes = "未读消息数量")
    @PostMapping("/unReadCount")
    public Result<Long> unReadCount() {
        return Result.ok(baseService.unReadCount());
    }
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "最新一条未读消息", notes = "最新一条未读消息")
    @PostMapping("/latestUnreadMessage/{type}")
    public Result<PcMessageVO> getLatestUnreadMessage(@PathVariable("type") String type,@RequestParam(value = "drillId",required = false) String drillId) {
        return Result.ok(baseService.getLatestUnreadMessage(type,drillId));
    }
    @ApiOperationSupport(order = 5)
    @GetMapping("/sendSocketMessage")
    public Result sendSocketMessage(String eventName) {
        baseService.sendSocketMessage(eventName);
        return Result.ok();
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = SwaggerConstants.SAVE_VALUE, notes = SwaggerConstants.SAVE_NOTES)
    @PostMapping("/save")
    public Result save(@RequestBody @Validated PcMessageSaveDTO dto) {
        if (ObjectUtil.isNotEmpty(dto.getUserList())){
            for (String userId : dto.getUserList()) {
                PcMessage message = BeanUtil.copyProperties(dto, PcMessage.class);
                message.setType(PcMessageConstant.TYPE_CUSTOMER);
                message.setAcceptId(userId);
                baseService.save(message);
                baseService.sendSocketMessage(message, CollUtil.set(false,userId));
            }
        }else {
            PcMessage message = BeanUtil.copyProperties(dto, PcMessage.class);
            message.setType(PcMessageConstant.TYPE_CUSTOMER);
            baseService.save(message);
            Set<String> dutyUserIdList = MsgUtil.getDutyUserIdList(dto.getPcDrillId());
            if (CollUtil.isNotEmpty(dutyUserIdList)){
                baseService.sendSocketMessage(message,dutyUserIdList);
            }
        }
        return Result.ok();
    }

}
